"""
Case Type   : cm_ctl可靠性
Case Name   : cm_ctl stop-n过程中kill -19 gaussdb进程
Create At   : 2025-03-1
Owner       : sungang14
Description :
    1: 数据库用户执行ps ux,查看进程;
    ps ux
    2: 执行cm_ctl stop -n命令;
    cm_ctl stop -n nodeid
    3: stop执行结束前,kill -19 gaussdb进程;
    kill -19 gaussdb进程
    4: 恢复进程,kill -18 gaussdb进程;
    kill -18 gaussdb进程
    5: 再次执行cm_ctl stop -n命令;
    cm_ctl stop -n nodeid
    6: 启动节点;
    cm_ctl start -n nodeid
Expect      :
    1: 成功
    2: stop进程夯住
    3: 成功
    4: stop成功
    5: 成功
    6: 成功
History     :
"""

import os
import time
import unittest

from testcase.utils.ComThread import ComThread
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class DefinedResource(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_user = Node('PrimaryDbUser')
        self.sta1_user = Node('Standby1Dbuser')
        self.constant = Constant()
        self.common = Common()

    def test_cm(self):
        text = '----step1 数据库用户执行ps ux,查看gaussdb进程; expect:获取进程id成功----'
        self.log.info(text)
        get_cmd = f"ps ux|grep 'gaussdb -D'|grep -v grep|tr -s ' '|cut -d ' ' -f 2"
        self.process_old = self.common.get_sh_result(self.sta1_user, get_cmd)
        self.log.info(self.process_old)
        self.assertNotEqual('', self.process_old, "执行失败:" + text)

        text = '----step2:执行cm_ctl stop -n命令,stop执行结束前,kill -19 gaussdb进程;expect:，kill 成功，stop进程hang住----'
        self.log.info(text)
        self.stop_cmd = f'source {macro.DB_ENV_PATH}; ' \
                   f'cm_ctl stop -n 2;'
        self.log.info(self.stop_cmd)
        stop_thread = ComThread(self.pri_user, args=(self.stop_cmd,))
        stop_thread.setDaemon(True)
        stop_thread.start()
        kill_cmd = f"kill -19 {self.process_old}"
        self.log.info(kill_cmd)
        kill_thread = ComThread(self.sta1_user.sh, args=(kill_cmd,))
        kill_thread.start()
        kill_thread.join(20)
        kill_res = kill_thread.get_result().result()
        self.log.info(kill_res)
        self.assertEquals("", kill_res, "执行失败:" + text)
        stop_thread.join(60)
        result = stop_thread.get_result()
        self.log.info(result)

        text = '----step3:执行cm_ctl query -Civ查询node状态;expect:状态为Standby Unknown,未成功stop----'
        self.log.info(text)
        time.sleep(10)
        query_cmd = f"source {macro.DB_ENV_PATH};cm_ctl query -Civ"
        kill_res = self.common.get_sh_result(self.pri_user, query_cmd)
        self.assertIn(f"6002     S Standby Unknown", kill_res, "执行失败:" + text)

        text = '----step4:恢复进程,kill -18 gaussdb进程;expect:成功----'
        self.log.info(text)
        kill_cmd = f"kill -18 {self.process_old}"
        kill_res = self.common.get_sh_result(self.sta1_user, kill_cmd)
        self.assertIn("", kill_res, "执行失败:" + text)
        time.sleep(5)

        text = '----step5: 再次执行cm_ctl stop -n命令; expect:stop成功----'
        self.log.info(text)
        self.process_old = self.common.get_sh_result(self.pri_user, self.stop_cmd)
        self.log.info(self.process_old)
        self.assertIn(f'cm_ctl: stop node successfully', self.process_old, "执行失败:" + text)

        text = '----step6:执行cm_ctl query -Civ查询node状态;expect:状态为Manually stopped----'
        self.log.info(text)
        kill_res = self.common.get_sh_result(self.pri_user, query_cmd)
        self.assertIn(f"6002     S Down    Unknown", kill_res, "执行失败:" + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text = '-----step7:启动节点; expect:成功-----'
        self.log.info(text)
        kill_cmd = f"kill -18 {self.process_old}"
        self.log.info(kill_cmd)
        self.common.get_sh_result(self.sta1_user, kill_cmd)
        start_cmd = f'source {macro.DB_ENV_PATH};' \
                    f'cm_ctl start -n 2;'
        self.log.info(start_cmd)
        start_msg = self.common.get_sh_result(self.pri_user, start_cmd)
        self.assertIn(self.constant.cm_start_node_success_msg, start_msg,
                      "执行失败:" + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
